package com.transpeed.payservice.pay.wechat;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.transpeed.payservice.Utils.PayCommonUtil;
import com.transpeed.payservice.Utils.XMLUtil;
import com.transpeed.payservice.Utils.XmlFormatter;
import com.transpeed.payservice.log.Log;
import com.transpeed.payservice.main.PostMessage;

public class QueryWeChatTrade {	
	
	public static String WeChat_trade_query(String TradeNumber) throws Exception {  
	        // 账号信息  
	        String appid = PayConfigUtil.APP_ID;  // appid  
	        //String appsecret = PayConfigUtil.APP_SECRET; // appsecret  
	        String mch_id = PayConfigUtil.MCH_ID; // 商业号  
	        String key = PayConfigUtil.API_KEY; // key  
	  
	        String out_trade_no =TradeNumber;//微信订单号
	        String currTime = PayCommonUtil.getCurrTime();  
	        String strTime = currTime.substring(8, currTime.length());  
	        String strRandom = PayCommonUtil.buildRandom(4) + "";  
	        String nonce_str = strTime + strRandom;  
	                
	          
	        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();  
	        packageParams.put("appid", appid);  
	        packageParams.put("mch_id", mch_id);  
	        packageParams.put("nonce_str", nonce_str);  
	        packageParams.put("out_trade_no", out_trade_no);  
	  
	        String sign = PayCommonUtil.createSign("UTF-8", packageParams,key);  
	        packageParams.put("sign", sign);  
	          
	        String requestXML = PayCommonUtil.getRequestXml(packageParams);  
	        Log.Write("收到上位机查询微信订单请求：\n" + new XmlFormatter().format(requestXML), "WeChat");
	   
	        String resXml = PostMessage.WeChatPost(requestXML ,PayConfigUtil.ORDERQUERY_URL);  
	          
	        Map map = XMLUtil.doXMLParse(resXml);  
	        String tradestatus = (String) map.get("trade_state");  
	          
	        return tradestatus;  
	}  

	}

